package com.yunhai.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.model.entity.FmsSpecificationItemConfigQuery;
import com.yunhai.model.entity.fms.FmsSpecificationItemConfigEntity;
import com.yunhai.model.entity.fms.FmsSpecificationItemConfigVo;
import com.yunhai.recipe.mapper.FmsSpecificationItemConfigMapper;
import com.yunhai.recipe.service.FmsCodeDictService;
import com.yunhai.recipe.service.FmsSpecificationItemConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FmsSpecificationItemConfigServiceImpl implements FmsSpecificationItemConfigService {


    @Resource
    private FmsSpecificationItemConfigMapper fmsSpecificationItemConfigMapper;
    @Resource
    private FmsCodeDictService fmsCodeDictService;
    /**
     * 获取 FmsSpecificationConfig 分页列表
     */
    @Transactional
    public IPage getFmsSpecificationItemConfigPagination(PaginationVo paginationVo, FmsSpecificationItemConfigQuery fmsSpecificationItemConfigQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<FmsSpecificationItemConfigEntity> lqs = new LambdaQueryWrapper();
        lqs.orderByDesc( FmsSpecificationItemConfigEntity::getCreateTime  );
        lqs.eq(fmsSpecificationItemConfigQuery.getEnabled()!=null, FmsSpecificationItemConfigEntity::getEnabled, fmsSpecificationItemConfigQuery.getEnabled());
        lqs.likeRight(fmsSpecificationItemConfigQuery.getNumber() !=null, FmsSpecificationItemConfigEntity::getNumber, fmsSpecificationItemConfigQuery.getNumber());
        lqs.likeRight(fmsSpecificationItemConfigQuery.getName() !=null, FmsSpecificationItemConfigEntity::getName, fmsSpecificationItemConfigQuery.getName());

        fmsSpecificationItemConfigMapper.selectPage(page, lqs);

        List<FmsSpecificationItemConfigEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 FmsSpecificationConfig 所有数据
     *
     */
    @Transactional
    public List<FmsSpecificationItemConfigEntity> getFmsSpecificationItemConfigAllList( FmsSpecificationItemConfigQuery fmsSpecificationItemConfigQuery ) throws Exception {

        LambdaQueryWrapper<FmsSpecificationItemConfigEntity> lqs = new LambdaQueryWrapper();
        List<FmsSpecificationItemConfigEntity> list = fmsSpecificationItemConfigMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 FmsSpecificationConfig 详情
     */
    @Transactional
    public FmsSpecificationItemConfigEntity getFmsSpecificationItemConfigDetail( Long id) throws Exception {

        FmsSpecificationItemConfigEntity fmsSpecificationItemConfig = fmsSpecificationItemConfigMapper.selectById(id);
        return fmsSpecificationItemConfig;
    }


    /**
     * 新增 FmsSpecificationConfig
     * */
    @Transactional
    public void addFmsSpecificationItemConfig(FmsSpecificationItemConfigVo fmsSpecificationItemConfigVo) throws Exception {

        FmsSpecificationItemConfigEntity fmsSpecificationItemConfigEntity = new FmsSpecificationItemConfigEntity();
        ReflectionCopy.copyProperties( fmsSpecificationItemConfigEntity, fmsSpecificationItemConfigVo );
        fmsSpecificationItemConfigMapper.insert(fmsSpecificationItemConfigEntity);

        fmsCodeDictService.updateById(2L, fmsSpecificationItemConfigVo.getNumber());
        return;
    }


    /**
     * 更新 FmsSpecificationConfig
     * */
    @Transactional
    public void updateFmsSpecificationItemConfig( FmsSpecificationItemConfigVo fmsSpecificationItemConfigVo) throws Exception {

        if( fmsSpecificationItemConfigVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        FmsSpecificationItemConfigEntity fmsSpecificationItemConfig = new FmsSpecificationItemConfigEntity();
        ReflectionCopy.copyProperties( fmsSpecificationItemConfig, fmsSpecificationItemConfigVo );
        fmsSpecificationItemConfigMapper.updateById(fmsSpecificationItemConfig);
        return;
    }

    /**
     * 删除 FmsSpecificationConfig
     * */
    @Transactional
    public void batchRemoveFmsSpecificationItemConfig( List<Long> ids ) throws Exception {
        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        fmsSpecificationItemConfigMapper.deleteBatchIds(ids);
    }
}
